package com.cxlin.cwanandroid.ui.blog.viewmodel

import androidx.lifecycle.viewModelScope
import com.cxlin.base.BaseViewModel
import com.cxlin.base.model.api.Result
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.ArticleList
import com.cxlin.cwanandroid.model.repository.CollectRepository
import com.cxlin.cwanandroid.model.repository.SystemRepository
import com.cxlin.cwanandroid.ui.login.viewmodel.LoginViewModel
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 9:34
 */
class BlogViewModel(private val repository: SystemRepository, private val collectRepository: CollectRepository) : BaseViewModel() {

    sealed class BlogUiState : UiState {
        object Idel : BlogUiState()
        data class IsRefresh(val result: Boolean) : BlogUiState()
        data class Articles(val result: ArticleList?) : BlogUiState()
    }

    sealed class BlogEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : BlogEvent()
    }

    sealed class BlogUiIntent : UiIntent {
        data class GetArticleList(val cid: Int, val isRefresh: Boolean = false) : BlogUiIntent()
        data class CollectArticle(val articleId: Int, val boolean: Boolean) : BlogUiIntent()
    }

    private var currentPage = 0


    override fun handleIntent(intent: UiIntent) {
        when (intent) {
            is BlogUiIntent.GetArticleList -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            currentPage = 0
                            mUiStateFlow.update { BlogUiState.IsRefresh(true) }
                        }
                        val result = repository.getSystemTypeDetail(intent.cid, currentPage)
                        if (result is Result.Success) {
                            mUiStateFlow.update { BlogUiState.IsRefresh(false) }
                            mUiStateFlow.update { BlogUiState.Articles(result.data) }
                            currentPage += 1
                        } else if (result is Result.Error) {
                            mUiStateFlow.update { BlogUiState.IsRefresh(false) }
                            mEffectFlow.emit(LoginViewModel.LoginEvent.ToastEvent(result.exception.message ?: "网络异常", true))
                        }
                    }
                }
            }

            is BlogUiIntent.CollectArticle -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.boolean) {
                            collectRepository.collectArticle(intent.articleId)
                        } else {
                            collectRepository.unCollectArticle(intent.articleId)
                        }

                    }
                }
            }
        }
    }

    override fun createInitState(): UiState {
        return BlogUiState.Idel
    }
}